{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Market Orders "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This section covers **market orders**, the most simple yet equally important order type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import fxcmpy\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "con = fxcmpy.fxcmpy(config_file='fxcm.cfg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Placing Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before one gets started, a check of the **open positions** might be helpful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Currently, there are no open positions. **To place orders**, one can use the methods `con.create_market_sell_order()` or `con.create_market_buy_order()`, respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order = con.create_market_sell_order('EUR/USD', 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order = con.create_market_buy_order('USD/JPY', 200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The new **open positions table** might then look as follows. Two new positions have been created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>1.21278</td>\n",
       "      <td>111.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>10</td>\n",
       "      <td>17.9199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>-1</td>\n",
       "      <td>-19.7138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isDisabled</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>1.21277</td>\n",
       "      <td>111.608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018135457</td>\n",
       "      <td>01122018135458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96429312</td>\n",
       "      <td>96429318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>usedMargin</th>\n",
       "      <td>1400</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>-0.1</td>\n",
       "      <td>-1.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0               1\n",
       "accountId             2815291         2815291\n",
       "accountName          02815291        02815291\n",
       "amountK                   100             200\n",
       "close                 1.21278         111.597\n",
       "com                         0               0\n",
       "currency              EUR/USD         USD/JPY\n",
       "currencyPoint              10         17.9199\n",
       "grossPL                    -1        -19.7138\n",
       "isBuy                   False            True\n",
       "isDisabled              False           False\n",
       "limit                       0               0\n",
       "open                  1.21277         111.608\n",
       "ratePrecision               5               3\n",
       "roll                        0               0\n",
       "stop                        0               0\n",
       "stopMove                    0               0\n",
       "t                           1               1\n",
       "time           01122018135457  01122018135458\n",
       "tradeId              96429312        96429318\n",
       "usedMargin               1400            2000\n",
       "valueDate                                    \n",
       "visiblePL                -0.1            -1.1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## General Market Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fxcm.py` allows to place more complex orders, this can be done with the method `con.open_trade()`. A detailed description of the meaning of the parameters is found in the <a href=\"https://fxcmapi.github.io/rest-api-docs/#operation/openTrade\">FXCM API Documentation</a>. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "order = con.open_trade(symbol='USD/JPY', is_buy=True,\n",
    "                       rate=105, is_in_pips=False,\n",
    "                       amount='1000', time_in_force='GTC', \n",
    "                       order_type='AtMarket', limit=120)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>1.21251</td>\n",
       "      <td>111.62</td>\n",
       "      <td>111.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>10</td>\n",
       "      <td>17.9163</td>\n",
       "      <td>89.5812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>26</td>\n",
       "      <td>21.5015</td>\n",
       "      <td>161.261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isDisabled</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>1.21277</td>\n",
       "      <td>111.608</td>\n",
       "      <td>111.602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018135457</td>\n",
       "      <td>01122018135458</td>\n",
       "      <td>01122018135507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96429312</td>\n",
       "      <td>96429318</td>\n",
       "      <td>96429362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>usedMargin</th>\n",
       "      <td>1400</td>\n",
       "      <td>2000</td>\n",
       "      <td>10000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>2.6</td>\n",
       "      <td>1.2</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0               1               2\n",
       "accountId             2815291         2815291         2815291\n",
       "accountName          02815291        02815291        02815291\n",
       "amountK                   100             200            1000\n",
       "close                 1.21251          111.62          111.62\n",
       "com                         0               0               0\n",
       "currency              EUR/USD         USD/JPY         USD/JPY\n",
       "currencyPoint              10         17.9163         89.5812\n",
       "grossPL                    26         21.5015         161.261\n",
       "isBuy                   False            True            True\n",
       "isDisabled              False           False           False\n",
       "limit                       0               0             120\n",
       "open                  1.21277         111.608         111.602\n",
       "ratePrecision               5               3               3\n",
       "roll                        0               0               0\n",
       "stop                        0               0               0\n",
       "stopMove                    0               0               0\n",
       "t                           1               1               1\n",
       "time           01122018135457  01122018135458  01122018135507\n",
       "tradeId              96429312        96429318        96429362\n",
       "usedMargin               1400            2000           10000\n",
       "valueDate                                                    \n",
       "visiblePL                 2.6             1.2             1.8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trade Ids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Every position has the attribute `tradeId`, here it is the column named `tradeId` of the `DataFrame` object returned by `con.get_open_positions()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    96429312\n",
       "1    96429318\n",
       "2    96429362\n",
       "Name: tradeId, dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions()['tradeId']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fxcm.py` provides methods to get the trade `tradeIds` directly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[96429312, 96429318, 96429362]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_trade_ids()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_closed_trade_ids()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[96429312, 96429318, 96429362]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_all_trade_ids()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Editing Market Orders"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To modify a trade, you need the `tradeId` value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tradeId = con.get_open_trade_ids()[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To **change the trades stop or limit rate**, one can use the `change_trade_stop_limit()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.change_trade_stop_limit(tradeId, is_in_pips=False,\n",
    "                            is_stop=False, rate=115)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As seen below, the limit rate of the last trade has changed to 115. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>1.21239</td>\n",
       "      <td>111.617</td>\n",
       "      <td>111.617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>10</td>\n",
       "      <td>17.9167</td>\n",
       "      <td>89.5837</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>38</td>\n",
       "      <td>16.1266</td>\n",
       "      <td>134.388</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isDisabled</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>1.21277</td>\n",
       "      <td>111.608</td>\n",
       "      <td>111.602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018135457</td>\n",
       "      <td>01122018135458</td>\n",
       "      <td>01122018135507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96429312</td>\n",
       "      <td>96429318</td>\n",
       "      <td>96429362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>usedMargin</th>\n",
       "      <td>1400</td>\n",
       "      <td>2000</td>\n",
       "      <td>10000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>3.8</td>\n",
       "      <td>0.9</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0               1               2\n",
       "accountId             2815291         2815291         2815291\n",
       "accountName          02815291        02815291        02815291\n",
       "amountK                   100             200            1000\n",
       "close                 1.21239         111.617         111.617\n",
       "com                         0               0               0\n",
       "currency              EUR/USD         USD/JPY         USD/JPY\n",
       "currencyPoint              10         17.9167         89.5837\n",
       "grossPL                    38         16.1266         134.388\n",
       "isBuy                   False            True            True\n",
       "isDisabled              False           False           False\n",
       "limit                       0               0             115\n",
       "open                  1.21277         111.608         111.602\n",
       "ratePrecision               5               3               3\n",
       "roll                        0               0               0\n",
       "stop                        0               0               0\n",
       "stopMove                    0               0               0\n",
       "t                           1               1               1\n",
       "time           01122018135457  01122018135458  01122018135507\n",
       "tradeId              96429312        96429318        96429362\n",
       "usedMargin               1400            2000           10000\n",
       "valueDate                                                    \n",
       "visiblePL                 3.8             0.9             1.5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Closing Positions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Positions can be closed individually via the `tradeId` ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.close_trade(trade_id=tradeId, amount=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>1.21252</td>\n",
       "      <td>111.621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>10</td>\n",
       "      <td>17.9162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>25</td>\n",
       "      <td>23.2931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isDisabled</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>1.21277</td>\n",
       "      <td>111.608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018135457</td>\n",
       "      <td>01122018135458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96429312</td>\n",
       "      <td>96429318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>usedMargin</th>\n",
       "      <td>1400</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>2.5</td>\n",
       "      <td>1.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0               1\n",
       "accountId             2815291         2815291\n",
       "accountName          02815291        02815291\n",
       "amountK                   100             200\n",
       "close                 1.21252         111.621\n",
       "com                         0               0\n",
       "currency              EUR/USD         USD/JPY\n",
       "currencyPoint              10         17.9162\n",
       "grossPL                    25         23.2931\n",
       "isBuy                   False            True\n",
       "isDisabled              False           False\n",
       "limit                       0               0\n",
       "open                  1.21277         111.608\n",
       "ratePrecision               5               3\n",
       "roll                        0               0\n",
       "stop                        0               0\n",
       "stopMove                    0               0\n",
       "t                           1               1\n",
       "time           01122018135457  01122018135458\n",
       "tradeId              96429312        96429318\n",
       "usedMargin               1400            2000\n",
       "valueDate                                    \n",
       "visiblePL                 2.5             1.3"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... or for all positions in an instrument ... "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "con.close_all_for_symbol('EUR/USD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountId</th>\n",
       "      <td>2815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>111.633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>17.9142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>44.7896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isDisabled</th>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>limit</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>111.608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stopMove</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <td>01122018135458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96429318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>usedMargin</th>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0\n",
       "accountId             2815291\n",
       "accountName          02815291\n",
       "amountK                   200\n",
       "close                 111.633\n",
       "com                         0\n",
       "currency              USD/JPY\n",
       "currencyPoint         17.9142\n",
       "grossPL               44.7896\n",
       "isBuy                    True\n",
       "isDisabled              False\n",
       "limit                       0\n",
       "open                  111.608\n",
       "ratePrecision               3\n",
       "roll                        0\n",
       "stop                        0\n",
       "stopMove                    0\n",
       "t                           1\n",
       "time           01122018135458\n",
       "tradeId              96429318\n",
       "usedMargin               2000\n",
       "valueDate                    \n",
       "visiblePL                 2.5"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... or simply for all positions over all instruments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "con.close_all()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are no open positions left."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The **closed positions** are found in the table returned by `con.get_closed_postions()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>accountName</th>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "      <td>02815291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>amountK</th>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>1000</td>\n",
       "      <td>200</td>\n",
       "      <td>100</td>\n",
       "      <td>1000</td>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>close</th>\n",
       "      <td>1.21277</td>\n",
       "      <td>1.21377</td>\n",
       "      <td>1.21373</td>\n",
       "      <td>111.136</td>\n",
       "      <td>111.136</td>\n",
       "      <td>1.21351</td>\n",
       "      <td>111.595</td>\n",
       "      <td>1.21306</td>\n",
       "      <td>111.622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>closeTime</th>\n",
       "      <td>01122018135435</td>\n",
       "      <td>01122018132307</td>\n",
       "      <td>01122018131730</td>\n",
       "      <td>01122018125855</td>\n",
       "      <td>01122018125856</td>\n",
       "      <td>01122018125856</td>\n",
       "      <td>01122018135528</td>\n",
       "      <td>01122018135544</td>\n",
       "      <td>01122018135549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>com</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currency</th>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "      <td>EUR/USD</td>\n",
       "      <td>USD/JPY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>currencyPoint</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>89.5676</td>\n",
       "      <td>17.9135</td>\n",
       "      <td>10</td>\n",
       "      <td>89.5676</td>\n",
       "      <td>10</td>\n",
       "      <td>17.9135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>grossPL</th>\n",
       "      <td>87</td>\n",
       "      <td>-29</td>\n",
       "      <td>-21</td>\n",
       "      <td>-179.96</td>\n",
       "      <td>-35.99</td>\n",
       "      <td>-25</td>\n",
       "      <td>-62.73</td>\n",
       "      <td>-29</td>\n",
       "      <td>25.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>isBuy</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open</th>\n",
       "      <td>1.21364</td>\n",
       "      <td>1.21348</td>\n",
       "      <td>1.21352</td>\n",
       "      <td>111.156</td>\n",
       "      <td>111.156</td>\n",
       "      <td>1.21326</td>\n",
       "      <td>111.602</td>\n",
       "      <td>1.21277</td>\n",
       "      <td>111.608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>openTime</th>\n",
       "      <td>01122018132410</td>\n",
       "      <td>01122018131348</td>\n",
       "      <td>01122018130602</td>\n",
       "      <td>01122018125852</td>\n",
       "      <td>01122018125851</td>\n",
       "      <td>01122018125850</td>\n",
       "      <td>01122018135507</td>\n",
       "      <td>01122018135457</td>\n",
       "      <td>01122018135458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ratePrecision</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>roll</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tradeId</th>\n",
       "      <td>96421953</td>\n",
       "      <td>96421202</td>\n",
       "      <td>96420641</td>\n",
       "      <td>96419876</td>\n",
       "      <td>96419864</td>\n",
       "      <td>96419858</td>\n",
       "      <td>96429362</td>\n",
       "      <td>96429312</td>\n",
       "      <td>96429318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>valueDate</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>visiblePL</th>\n",
       "      <td>8.7</td>\n",
       "      <td>-2.9</td>\n",
       "      <td>-2.1</td>\n",
       "      <td>-2</td>\n",
       "      <td>-2</td>\n",
       "      <td>-2.5</td>\n",
       "      <td>-0.7</td>\n",
       "      <td>-2.9</td>\n",
       "      <td>1.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            0               1               2               3  \\\n",
       "accountName          02815291        02815291        02815291        02815291   \n",
       "amountK                   100             100             100            1000   \n",
       "close                 1.21277         1.21377         1.21373         111.136   \n",
       "closeTime      01122018135435  01122018132307  01122018131730  01122018125855   \n",
       "com                         0               0               0               0   \n",
       "currency              EUR/USD         EUR/USD         EUR/USD         USD/JPY   \n",
       "currencyPoint              10              10              10         89.5676   \n",
       "grossPL                    87             -29             -21         -179.96   \n",
       "isBuy                   False           False           False            True   \n",
       "open                  1.21364         1.21348         1.21352         111.156   \n",
       "openTime       01122018132410  01122018131348  01122018130602  01122018125852   \n",
       "ratePrecision               5               5               5               3   \n",
       "roll                        0               0               0               0   \n",
       "t                           2               2               2               2   \n",
       "tradeId              96421953        96421202        96420641        96419876   \n",
       "valueDate                                                                       \n",
       "visiblePL                 8.7            -2.9            -2.1              -2   \n",
       "\n",
       "                            4               5               6               7  \\\n",
       "accountName          02815291        02815291        02815291        02815291   \n",
       "amountK                   200             100            1000             100   \n",
       "close                 111.136         1.21351         111.595         1.21306   \n",
       "closeTime      01122018125856  01122018125856  01122018135528  01122018135544   \n",
       "com                         0               0               0               0   \n",
       "currency              USD/JPY         EUR/USD         USD/JPY         EUR/USD   \n",
       "currencyPoint         17.9135              10         89.5676              10   \n",
       "grossPL                -35.99             -25          -62.73             -29   \n",
       "isBuy                    True           False            True           False   \n",
       "open                  111.156         1.21326         111.602         1.21277   \n",
       "openTime       01122018125851  01122018125850  01122018135507  01122018135457   \n",
       "ratePrecision               3               5               3               5   \n",
       "roll                        0               0               0               0   \n",
       "t                           2               2               2               2   \n",
       "tradeId              96419864        96419858        96429362        96429312   \n",
       "valueDate                                                                       \n",
       "visiblePL                  -2            -2.5            -0.7            -2.9   \n",
       "\n",
       "                            8  \n",
       "accountName          02815291  \n",
       "amountK                   200  \n",
       "close                 111.622  \n",
       "closeTime      01122018135549  \n",
       "com                         0  \n",
       "currency              USD/JPY  \n",
       "currencyPoint         17.9135  \n",
       "grossPL                 25.08  \n",
       "isBuy                    True  \n",
       "open                  111.608  \n",
       "openTime       01122018135458  \n",
       "ratePrecision               3  \n",
       "roll                        0  \n",
       "t                           2  \n",
       "tradeId              96429318  \n",
       "valueDate                      \n",
       "visiblePL                 1.4  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_closed_positions().T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The `fxcm_open_position` Class"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For convenience, `fxcm` provides a class called `fxcm_open_positions`. All open positions are stored in the attribute `fxcm.open_pos`, a dictionary with the position's `tradeId` as key and the `fxcm_open_position` instance as value. Since there are no open positions left, the `dict` object is empty."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.open_pos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Therefore, create an order ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "order = con.create_market_sell_order('EUR/USD', 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... to see the `fxcm_open_position` objects in the `dict` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{96429674: <fxcm.fxcm_open_position at 0x7fa21c072780>}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.open_pos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get a single `fxcm_open_position` object, one can use `con.get_open_position()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tradeId = con.get_open_trade_ids()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pos = con.get_open_position(tradeId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<fxcm.fxcm_open_position at 0x7fa21c072780>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accountId:        2815291\n",
      "accountName:      02815291\n",
      "amountK:          100\n",
      "close:            1.21223\n",
      "com:              0\n",
      "currency:         EUR/USD\n",
      "currencyPoint:    10\n",
      "grossPL:          1\n",
      "isBuy:            False\n",
      "isDisabled:       False\n",
      "limit:            0\n",
      "open:             1.21224\n",
      "roll:             0\n",
      "stop:             0\n",
      "stopMove:         0\n",
      "time:             2018-01-12 13:56:12\n",
      "tradeId:          96429674\n",
      "usedMargin:       1400\n",
      "valueDate:        \n",
      "visiblePL:        0.1\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `fxcm_open_position` object has `get` methods for all of its attributes, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos.get_amount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'EUR/USD'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pos.get_currency()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Close the position** with the `pos.close()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pos.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.get_open_positions()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
